package com.farm.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.farm.aop.Operation;
import com.farm.aop.Type;
import com.farm.aop.aopUpdate;
import com.farm.filter.SusceptibleFilter;
import com.farm.pojo.Logs;
import com.farm.pojo.Policy;
import com.farm.pojo.User;
import com.farm.service.LogsService;
import com.farm.service.PolicyService;
import com.farm.common.AppHttpCodeEnum;
import com.farm.common.ResponseResult;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.nlp.tokenizers.ChineseWordTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 存放政策文件 前端控制器
 * </p>
 *
 * @author farm
 * @since 2023-11-14
 */
@RestController
@RequestMapping("/policy")
public class PolicyController {
    @Autowired
    private PolicyService policyService;

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Autowired
    private RedisTemplate<Object,Object> template;

    @Autowired
    private SusceptibleFilter susceptibleFilter;


    @Autowired
    private LogsService logsService;
    public void logsKeep(){
        Logs logs = new Logs();
        logs.setOperate("对资源进行操作");
        logsService.save(logs);
    }
    @GetMapping("/admin/getlist")
    public ResponseResult getPolicyList(){
        return ResponseResult.okResult(policyService.list());
    }

    @GetMapping("/list")
    public ResponseResult getList(){
        return ResponseResult.okResult(policyService.list(Wrappers.<Policy>lambdaQuery().eq(Policy::getState,1)));
    }

    @GetMapping("/show")
    public ResponseResult getListLimit(){
        return ResponseResult.okResult(policyService.list(Wrappers.<Policy>lambdaQuery().eq(Policy::getState,1).last("LIMIT 5")));
    }

    @PostMapping("/admin/changeState")
//    @aopUpdate(TypeValue = Type.POLICY, OperationValue = Operation.UPDATE)
    public ResponseResult changeState(@RequestBody Policy policy){
//        template.opsForValue().set(policy.getId() + "", JSON.toJSONString(policy));
//        kafkaTemplate.send("farm.policy",policy.getId().toString());
        policyService.updateById(policy);
        logsKeep();
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @PostMapping("/admin/update")
//    @aopUpdate(TypeValue = Type.POLICY, OperationValue = Operation.UPDATE)
    public ResponseResult updateUserStatus(@RequestBody Policy policy){
        policyService.updateById(policy);
        logsKeep();
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @PostMapping("/admin/push")
    public ResponseResult adminPush(@RequestBody Policy policy){
        String content = policy.getContent();
        FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        // 设置分词返回数量(频率最高的600个词)
        frequencyAnalyzer.setWordFrequenciesToReturn(600);
        // 最小分词长度
        frequencyAnalyzer.setMinWordLength(2);
        // 引入中文解析器
        frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
        List<String> list = new ArrayList<>();
        list.add(content);
        final List<WordFrequency> wordFrequencies = frequencyAnalyzer.load(list);
        int size = wordFrequencies.size();
        StringBuilder stringBuilder = new StringBuilder();
        if(size < 3 && size > 0){
            for(int i = 0;i<size;i++){
                stringBuilder.append(wordFrequencies.get(i).getWord());
                stringBuilder.append(',');
            }
        }else if(size == 0){
            stringBuilder.append("");
        }else {
            for(int i = 0;i<3;i++){
                stringBuilder.append(wordFrequencies.get(i).getWord());
                stringBuilder.append(',');
            }
        }
        policy.setLabel(stringBuilder.toString());
        policy.setContent(susceptibleFilter.WordFilter(policy.getContent()));
        policyService.save(policy);
        logsKeep();
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @PostMapping("/getmylist")
    public ResponseResult getmMyList(@RequestBody User user){
        return ResponseResult.okResult(policyService.list(Wrappers.<Policy>lambdaQuery().eq(Policy::getUserId,user.getUserId())));
    }



    @PostMapping("/deletemylist")
//    @aopUpdate(TypeValue = Type.POLICY, OperationValue = Operation.DELETE)
    public ResponseResult deleteMyList(@RequestBody Policy policy){
        policyService.removeById(policy.getId());
        logsKeep();
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @PostMapping("/put")
    public ResponseResult putData(@RequestBody Policy policy){
        String content = policy.getContent();
        System.out.println(content);
        FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        // 设置分词返回数量(频率最高的600个词)
        frequencyAnalyzer.setWordFrequenciesToReturn(600);
        // 最小分词长度
        frequencyAnalyzer.setMinWordLength(2);
        // 引入中文解析器
        frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
        List<String> list = new ArrayList<>();
        list.add(content);
        final List<WordFrequency> wordFrequencies = frequencyAnalyzer.load(list);
        int size = wordFrequencies.size();
        StringBuilder stringBuilder = new StringBuilder();
        if(size < 3 && size > 0){
            for(int i = 0;i<size;i++){
                stringBuilder.append(wordFrequencies.get(i).getWord());
                stringBuilder.append(',');
            }
        }else if(size == 0){
            stringBuilder.append("");
        }else {
            for(int i = 0;i<3;i++){
                stringBuilder.append(wordFrequencies.get(i).getWord());
                stringBuilder.append(',');
            }
        }
        policy.setLabel(stringBuilder.toString());
        policyService.save(policy);
        logsKeep();
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}

